package org.xue.snowmall.order.service.impl;

import com.alibaba.fastjson.TypeReference;
import entity.GoodsEntity;
import entity.GroupEntity;
import entity.UserRecordEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.xue.snowmall.common.utils.R;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.xue.snowmall.common.utils.PageUtils;
import org.xue.snowmall.common.utils.Query;

import org.xue.snowmall.order.dao.GoodsOrderDao;
import entity.GoodsOrderEntity;
import org.xue.snowmall.order.feign.GoodsFeignService;
import org.xue.snowmall.order.feign.GroupFeignService;
import org.xue.snowmall.order.service.GoodsOrderService;

import javax.servlet.http.HttpSession;


@Service("goodsOrderService")
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderDao, GoodsOrderEntity> implements GoodsOrderService {
    public static ThreadLocal<UserRecordEntity> loginUser = new ThreadLocal<>();
    @Autowired
    private HttpSession session;

    @Autowired
    private GroupFeignService groupFeignService;


    @Autowired
    private GoodsFeignService goodsFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsOrderEntity> page = this.page(
                new Query<GoodsOrderEntity>().getPage(params),
                new QueryWrapper<GoodsOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R saveOrder(Map<String, Object> params) {
        Long psmId = new Long((String) params.get("psmId"));
        //根据id查询所有的商品 feign调用
        GoodsEntity goods = goodsFeignService.findByPsmId(psmId);

        //添加订单
        String gsmGroupId = String.valueOf(params.get("gsmGroupId"));
        String gsmBargainId = String.valueOf(params.get("gsmBargainId"));
        GoodsOrderEntity goodsOrderEntity = new GoodsOrderEntity();
            goodsOrderEntity.setGsmGroupId("0");
            goodsOrderEntity.setGsmBargainId("0");
            goodsOrderEntity.setGsmSeckillId("0");
        if (goodsOrderEntity.getPsmBuyCount().equals(0)) {
            goodsOrderEntity.setPsmBuyCount(1);
        }
        //获取用户id
        UserRecordEntity userRecordEntity = (UserRecordEntity) session.getAttribute("user");
        goodsOrderEntity.setUsmId(userRecordEntity.getUsmId());
        goodsOrderEntity.setPsmId(psmId);
        goodsOrderEntity.setPsmName(goods.getPsmName());
        goodsOrderEntity.setPsmImg(goods.getPsmImg());
        goodsOrderEntity.setPsmPrice(goods.getPsmPrice());
        goodsOrderEntity.setOsmCreatTime(new Date());
        goodsOrderEntity.setOsmTotalPrice(new BigDecimal(goodsOrderEntity.getPsmBuyCount()).multiply(goodsOrderEntity.getPsmPrice()));
        //购买数量默认为一
        goodsOrderEntity.setPsmBuyCount(1);
        UUID uuid = UUID.randomUUID();
        String replace = uuid.toString().replace("_", "");
        String s = LocalDate.now().toString();
        String replace1 = s.replace("_", "");
        goodsOrderEntity.setOsmCode(replace1 + replace);
        goodsOrderEntity.setOsmPayStatus("0");
        baseMapper.insert(goodsOrderEntity);

        return R.ok().put("osmId", goodsOrderEntity.getOsmId());
    }

    @Override
    public List<GoodsOrderEntity> findByUsmId() {

        UserRecordEntity user = (UserRecordEntity) session.getAttribute("user");

        return baseMapper.selectList(new QueryWrapper<GoodsOrderEntity>().eq("usm_id", user.getUsmId()));
    }

    @Override
    public GoodsOrderEntity findByOsmId(Map<String, Object> params) {
        Long osmId = new Long((String) params.get("osmId"));
        //查出当前的订单信息
        return baseMapper.selectById(osmId);
    }

    @Override
    public R createGroupOrder(Long gsmGroupId) {

        //feign调用根据gsmGroupId查出所有活动的信息
        R info = groupFeignService.info(gsmGroupId);
        GroupEntity group = info.getData("group", new TypeReference<GroupEntity>() {
        });
        GoodsOrderEntity order = new GoodsOrderEntity();


        order.setGsmGroupId(gsmGroupId + "");
        order.setGsmSeckillId("0");
        order.setGsmBargainId("0");
        //获取用户id
        UserRecordEntity userRecordEntity = (UserRecordEntity) session.getAttribute("user");
        order.setUsmId(userRecordEntity.getUsmId());
        order.setPsmId(group.getPsmId());
        order.setPsmName(group.getPsmName());
        order.setPsmPrice(group.getPsmPrice());
        order.setOsmCreatTime(new Date());
        if (order.getPsmBuyCount().equals(0)) {
            order.setPsmBuyCount(1);
        }
        UUID uuid = UUID.randomUUID();
        String replace = uuid.toString().replace("_", "");
        String s = LocalDate.now().toString();
        String replace1 = s.replace("_", "");
        BigDecimal gsmGroupPrice = order.getGsmGroupPrice();
        Integer psmBuyCount = order.getPsmBuyCount();
        BigDecimal bigDecimal = new BigDecimal(psmBuyCount);
        order.setOsmTotalPrice(bigDecimal.multiply(gsmGroupPrice));
        order.setOsmCode(replace1 + replace);
        order.setOsmPayStatus("0");
        order.setPsmImg(group.getPsmImg());
        order.setGsmGroupPrice(group.getGsmGroupPrice());
        baseMapper.insert(order);
        return R.ok().put("osmId", order.getOsmId());
    }


}